package com.gitee.hljdrl.utils.network;

import com.gitee.hljdrl.utils.string.StringUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NetWorkUtil {

    /**
     * @param count 次数
     * @param ipAddress id地址
     * @return 返回ping结果
     */
    public static String pingSync(int count, String ipAddress) {
        String pingCommand = StringUtil.string("ping -c ", count, " ", ipAddress);
        StringBuilder output = new StringBuilder();
        Runtime runtime = Runtime.getRuntime();
        try {
            Process process = runtime.exec(pingCommand);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        output.append("\n");
        return output.toString();
    }
}
